package com.xxq.semester.controller.exam;


import com.xxq.semester.entity.utils.ResponseEntity;
import com.xxq.semester.service.exam.IStudentAnswersService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

/**
 * <p>
 * 学生答案控制器
 * </p>
 *
 * @author saki
 * @since 2025-07-13
 */
@RestController
@RequestMapping("/api/student-answers")
@Tag(name = "学生答案管理", description = "学生提交答案相关操作")
public class StudentAnswersController {

    @Autowired
    private IStudentAnswersService studentAnswersService;

    /**
     * 提交单个题目的答案
     */
    @PostMapping("/submit-single")
    @Operation(summary = "提交单个答案", description = "学生提交单个题目的答案")
    public ResponseEntity<Map<String, Object>> submitSingleAnswer(
            @RequestBody @Parameter(description = "单个答案提交请求") SingleAnswerRequest request) {

        studentAnswersService.submitAnswer(
                request.getStudentId(),
                request.getExamId(),
                request.getQuestionId(),
                request.getAnswer()
        );

        Map<String, Object> response = new HashMap<>();
        response.put("message", "答案提交成功");
        response.put("studentId", request.getStudentId());
        response.put("examId", request.getExamId());
        response.put("questionId", request.getQuestionId());

        return ResponseEntity.success(response);
    }

    /**
     * 批量提交答案
     */
    @PostMapping("/submit-batch")
    @Operation(summary = "批量提交答案", description = "学生批量提交考试答案")
    public ResponseEntity<Map<String, Object>> submitBatchAnswers(
            @RequestBody @Parameter(description = "批量答案提交请求") BatchAnswerRequest request) {

        studentAnswersService.submitAnswers(
                request.getStudentId(),
                request.getExamId(),
                request.getAnswers()
        );

        Map<String, Object> response = new HashMap<>();
        response.put("message", "答案批量提交成功");
        response.put("studentId", request.getStudentId());
        response.put("examId", request.getExamId());
        response.put("submittedCount", request.getAnswers().size());

        return ResponseEntity.success(response);
    }

    /**
     * 单个答案提交请求类
     */
    @Setter
    @Getter
    public static class SingleAnswerRequest {
        // getter和setter
        private Integer studentId;
        private Integer examId;
        private Integer questionId;
        private String answer;

    }

    /**
     * 批量答案提交请求类
     */
    @Setter
    @Getter
    public static class BatchAnswerRequest {
        // getter和setter
        private Integer studentId;
        private Integer examId;
        private List<IStudentAnswersService.AnswerSubmission> answers;

    }
}
